#include <iostream>
#include <string>
#include <memory>
#include <cstring>
#include <memory.h>
using namespace std;

class Data
{
private:
public:
    Data()
    {
        cout << "Data()" << endl;
    }
    ~Data()
    {
        cout << "~Data()" << endl;
    }

    void Print()
    {
        cout << "Data Print" << endl;
    }
};

class DataPtr
{
private:
    Data *ptr_{nullptr};

public:
    // 恢复默认构造函数
    DataPtr() = default;

    // 限制使用拷贝构造
    DataPtr(const DataPtr &dp) = delete;

    DataPtr(DataPtr &&dp)
    {
        ptr_ = dp.ptr_;
        dp.ptr_ = nullptr;
    };

    DataPtr(Data *ptr) : ptr_(ptr) {}

    ~DataPtr()
    {
        delete ptr_;
        ptr_ = nullptr;
    }

public:
    // 限制使用拷贝赋值
    DataPtr &operator=(const DataPtr &) = delete;
    // 限制使用移动赋值
    DataPtr &operator=(DataPtr &&dp)
    {
        if (dp.ptr_ == ptr_)
            return *this;

        delete ptr_;

        ptr_ = dp.ptr_;
        dp.ptr_ = nullptr;

        return *this;
    };

    Data *get() { return ptr_; }

    Data *release()
    {
        auto p = ptr_;
        ptr_ = nullptr;
        return p;
    }
};

int main(int argc, char const *argv[])
{
    DataPtr ptr(new Data);
    DataPtr ptr2;
    // ptr2 = ptr; // 已被限制，无法使用
    ptr2 = move(ptr);
    return 0;
}
